package thread_test;

/**
 * @author ten_fei
 * @create 2021-02-17 21:54
 */

/**
 * 银行有一个账户
 * 有两个储户风别向同一个账户存3000元，每次存1000，存三次，每次存完打印账户余额。
 */

class Account {
    private double balance;

    public Account(double balance) {
        this.balance = balance;
    }

    //存钱
    public synchronized void deposit(double amt) {
        if (amt > 0 && balance < 30000) {
            System.out.println("存钱中...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            balance += amt;
            System.out.println(Thread.currentThread().getName() + "存钱成功。余额为：" + balance);
        }
    }
}

class Customer extends Thread {
    private Account acct;

    public Customer(Account acct) {
        this.acct = acct;
    }

    public void run() {
        for (int i = 0; i < 30; i++) {
            acct.deposit(1000);
        }
    }
}


public class ThreadTest01 {
    public static void main(String[] args) {
        Account acct = new Account(0);
        Customer c1 = new Customer(acct);
        Customer c2 = new Customer(acct);

        c1.setName("甲");
        c2.setName("乙");

        c1.start();
        c2.start();
    }
}









